package me.ingxin.android.activitylauncher.app

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import me.ingxin.android.activitylauncher.app.databinding.FragmentMainBinding
import me.ingxin.android.activitylauncher.launchActivityForResult

/**
 * Created by ingxin on 2022/4/21
 */
class MainFragment : Fragment() {

    private lateinit var binding: FragmentMainBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMainBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnStart.setOnClickListener {
//            launchActivityForResult(TargetActivity::class.java) {
//                val str = it.data?.getStringExtra("result_str") ?: "没有返回值"
//                binding.tvShow.text = "fragment结果:$str"
//            }
            launchActivityForResult<Intent, ActivityResult>(
                Intent(requireContext(), TargetActivity::class.java),
                ActivityResultContracts.StartActivityForResult(),
                null
            ){
                val str = it.data?.getStringExtra("result_str") ?: "没有返回"
                binding.tvShow.text = "fragment结果:$str"
            }
        }
    }

}